
package com.yimixian.app.ui;

import com.yimixian.app.R;

import android.content.Context;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

public class ProgressLineView extends LinearLayout {
    private LinearLayout mDrawableLinearLayout;
    private LinearLayout mTextLinearLayout;
    private int mBallMargin;
    private int mLinearLayoutPadding;
    private int mBallSize;

    public ProgressLineView(Context context) {
        super(context);
        initProgressLineView();
    }

    public ProgressLineView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initProgressLineView();
    }

    public ProgressLineView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initProgressLineView();
    }

    private void initProgressLineView() {
        mLinearLayoutPadding = getResources().getDimensionPixelOffset(
                R.dimen.progress_line_drawable_linearLayout_padding);
        mBallMargin = getResources().getDimensionPixelOffset(R.dimen.progress_line_ball_margin);
        mBallSize = getResources().getDimensionPixelOffset(R.dimen.progress_line_ball_size);
        setOrientation(VERTICAL);
        mDrawableLinearLayout = new LinearLayout(getContext());
        mDrawableLinearLayout.setOrientation(HORIZONTAL);
        mDrawableLinearLayout.setPadding(mLinearLayoutPadding, 0, mLinearLayoutPadding, 0);
        mTextLinearLayout = new LinearLayout(getContext());
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        mTextLinearLayout.setLayoutParams(layoutParams);
        mDrawableLinearLayout.setOrientation(HORIZONTAL);
        addView(mDrawableLinearLayout);
        addView(mTextLinearLayout);
    }

    public void setProgressLineItems(List<String> items, int currentItem, int status) {
        mDrawableLinearLayout.removeAllViews();
        mTextLinearLayout.removeAllViews();
        for (int i = 0; i < items.size(); i++) {
            ImageView ballImageView = new ImageView(getContext());
            LinearLayout.LayoutParams ballLayoutParams = new LinearLayout.LayoutParams(mBallSize,
                    mBallSize);
            ballLayoutParams.gravity = Gravity.CENTER_VERTICAL;
            ballLayoutParams.setMargins(mBallMargin, 0, mBallMargin, 0);
            ballImageView.setLayoutParams(ballLayoutParams);
            if (status == 1){//有效订单
                if (i > currentItem) {
                    ballImageView.setImageResource(R.drawable.order_list_grey_ball);
                } else {
                    ballImageView.setImageResource(R.drawable.order_list_orange_ball);
                }
            }else{//无效订单
                ballImageView.setImageResource(R.drawable.order_list_orange_ball_invalid);
            }

            mDrawableLinearLayout.addView(ballImageView);

            if (i != items.size() - 1) {
                for (int j = 0; j < 2; j++) {
                    ImageView lineImageView = new ImageView(getContext());
                    LinearLayout.LayoutParams lineLayoutParams = new LinearLayout.LayoutParams(
                            ViewGroup.LayoutParams.WRAP_CONTENT,
                            ViewGroup.LayoutParams.WRAP_CONTENT);
                    lineLayoutParams.gravity = Gravity.CENTER_VERTICAL;
                    lineImageView.setLayoutParams(lineLayoutParams);
                    lineImageView.setImageResource(R.drawable.order_list_grey_line);
                    mDrawableLinearLayout.addView(lineImageView);
                }
            }
            TextView textView = new TextView(getContext());
            textView.setText(items.get(i));
            textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 9.0f);
            textView.setGravity(Gravity.CENTER);
            if (status == 1){//有效订单
                if (i > currentItem) {
                    textView.setTextColor(0xff999999);
                } else {
                    textView.setTextColor(getResources().getColor(R.color.ymx_orange));
                }
            }else{//无效订单
                textView.setTextColor(getResources().getColor(R.color.cart_item_unit_des));
            }

            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(0,
                    ViewGroup.LayoutParams.WRAP_CONTENT, 1);
            textView.setLayoutParams(layoutParams);
            mTextLinearLayout.addView(textView);
        }

    }

}
